//
//  HREmotionCollectionViewCell.swift
//  weiBo
//
//  Created by helong on 16/7/20.
//  Copyright © 2016年 helong. All rights reserved.
//
/// emotionCollectionView的自定义cell
import UIKit

class HREmotionCollectionViewCell: UICollectionViewCell {
    //  cell的数据源
    var emotion: [HREmotion]? {
        didSet{
            guard let emotionArray = emotion else{
                return
            }
            //  防止cell中button重用,造成没有内容的cell也显示内容的bug,先将按钮全部影藏,如果有内容的cell再显示button
            for btn in buttonArray {
                btn.hidden = true
            }
            //  如果是PNG图
            for (i, value) in emotionArray.enumerate() {
                //  给按钮的模型赋值
                buttonArray[i].emotion = value
                if value.type == "0" {
                buttonArray[i].setImage(UIImage(named: value.absolutePathForPNG!), forState: .Normal)
                    //  设置title为nil 防止图片显示不正常
                    buttonArray[i].setTitle(nil, forState: .Normal)
                }else {
                    let emojiString = value.code!.emoji()
                    //  设置图像为nil,防止显示title不正常
                    buttonArray[i].setImage(nil, forState: .Normal)
                    //  设置文字的大小(就是emoji表情的大小)
                    buttonArray[i].titleLabel?.font = UIFont.systemFontOfSize(37)
                    //  设置文字为emoji
                    buttonArray[i].setTitle(emojiString, forState: .Normal)
                }
                //  显示有内容的cell上的button
                buttonArray[i].hidden = false
            }
        }
    }
    
    //  懒加载按钮数组
    private lazy var buttonArray:[HREmotionButton] = [HREmotionButton]()
    
    //  懒加载删除按钮
    private lazy var deleteButton: UIButton = {
        
        let btn = UIButton()
        btn.setImage(UIImage(named: "compose_emotion_delete"), forState: .Normal)
        btn.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: .Highlighted)
        btn.backgroundColor = UIColor.whiteColor()
        btn.addTarget(self, action: "deleteButtonClicked", forControlEvents: .TouchUpInside)
//        btn.sizeToFit()
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
     // MARK: - 设置UI
    private func setupUI(){
//        backgroundColor = randomColor()
        printLog()
        //  在每个cell中创建21个button
        addChildButton()
        contentView.addSubview(deleteButton)
//        deleteButton.snp_makeConstraints { (make) -> Void in
//            //  为什么约束不对?????????????????????????????
//            make.trailing.equalTo(contentView)
//            make.bottom.equalTo(contentView)
//            
//        }
    }
    
     // MARK: - 计算按钮的frame
    override func layoutSubviews() {
        super.layoutSubviews()
        let itemWidth = self.width / 7
        let itemHeight = self.height / 3
        deleteButton.bounds.size = CGSize(width: itemWidth, height: itemHeight)
        print(deleteButton)
        for (i, value) in buttonArray.enumerate() {
            let cols = i % 7
            let rows = i / 7
            
            value.bounds.size = CGSize(width: itemWidth, height: itemHeight)
            value.x = itemWidth * CGFloat(cols)
            value.y = itemHeight * CGFloat(rows)
        }
        deleteButton.bounds.size = CGSize(width: itemWidth, height: itemHeight)
        deleteButton.x = self.width - itemWidth
        deleteButton.y = self.height - itemHeight

    }
    
     // MARK: - 创建按钮的方式
    private func addChildButton(){
        for _ in 0..<20 {
            let btn = HREmotionButton()
            //  添加点击事件
            btn.addTarget(self, action: "selectEmotion:", forControlEvents: .TouchUpInside)
            printLog()
            buttonArray.append(btn)
            contentView.addSubview(btn)
        }
}
    
     // MARK: - 表情按钮的点击事件
    /**
    //  点击按钮的时候要拿到被点击按钮对应的模型,所以我们自定义按钮,让其持有模型,创建的时候为模型赋值
    
    - parameter sender: 被点击的按钮
    */
    @objc private func selectEmotion(sender: HREmotionButton){
        //  使用通知传值,通知textView显示按钮的内容
        NSNotificationCenter.defaultCenter().postNotificationName(EmotionSelectedNotification, object: sender)
        
        
        
    }
    
     // MARK: - 删除按钮的点击事件
    @objc private func deleteButtonClicked() {
        print("删除按钮的点击事件")
    }
}
